{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from ipywidgets import interact, interactive, FloatSlider, IntSlider, ToggleButtons\n",
    "from geoscilabs.em.TDEMGroundedSource import choose_model, load_or_run_results, PlotTDEM\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Exploring fields from a grounded source"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Purpose\n",
    "\n",
    "We explore time-domain electromagnetic (EM) simulation results from a grounded source. Both electric currents and magnetic flux will be visualized to undertand physics of grounded source EM. Both charge buildup (galvanic) and EM induction (inductive) will occur at different times. "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Load simulation results (or run)\n",
    "\n",
    "Three models are considered here. \n",
    "\n",
    "- Halfspace (0.01 S/m)\n",
    "- Conductive block in halfspace (1 S/m)\n",
    "- Resitive block in halfspace (10$^{-4}$ S/m)\n",
    "\n",
    "Using below widget, you can choose a model that you want to explore. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "ddc3e6c271bc44b7ab04d31d1af258a9",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "interactive(children=(ToggleButtons(description='model', options=('halfspace', 'conductor', 'resistor'), value…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "Q = interact(choose_model, \n",
    "         model=ToggleButtons(\n",
    "             options=[\"halfspace\", \"conductor\", \"resistor\"], value=\"halfspace\"\n",
    "         )\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Then here we are going to load results. If you want to rerun, you can set `re_run` as `True`. \n",
    "With that option, you can change conductivity value of the block and halfspace you can alter values for `sigma_halfspace` and `sigma_block`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "file already exists, new file is called /Users/lindseyjh/git/geosci/geosci-labs/notebooks/em/tdem_groundedsource.tar\n",
      "Downloading https://storage.googleapis.com/simpeg/em_examples/tdem_groundedsource/tdem_groundedsource.tar\n",
      "   saved to: /Users/lindseyjh/git/geosci/geosci-labs/notebooks/em/tdem_groundedsource.tar\n",
      "Download completed!\n"
     ]
    }
   ],
   "source": [
    "import matplotlib\n",
    "matplotlib.rcParams['font.size']=16\n",
    "options = load_or_run_results(\n",
    "    re_run=False, \n",
    "    fname=choose_model(Q.widget.kwargs['model']),\n",
    "    sigma_block=0.01,\n",
    "    sigma_halfspace=0.01\n",
    ")\n",
    "tdem = PlotTDEM(**options)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "a8386bb598eb410aa4d71af3bf82970a",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "interactive(children=(FloatSlider(value=30.0, description='elev', max=180.0, min=-180.0, step=10.0), FloatSlid…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "<function ipywidgets.widgets.interaction._InteractFactory.__call__.<locals>.<lambda>(*args, **kwargs)>"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "interact(\n",
    "    tdem.show_3d_survey_geometry, \n",
    "    elev=FloatSlider(min=-180, max=180, step=10, value=30),\n",
    "    azim=FloatSlider(min=-180, max=180, step=10, value=-45),\n",
    ")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "5c7bceeeee944f01b2b0d6b3667bbd67",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "interactive(children=(IntSlider(value=15, continuous_update=False, description='itime', max=50, min=15), Toggl…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "<function ipywidgets.widgets.interaction._InteractFactory.__call__.<locals>.<lambda>(*args, **kwargs)>"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "interact(\n",
    "    tdem.plot_input_currents, \n",
    "    itime=IntSlider(min=15, max=50, step=1, value=15, continuous_update=False),\n",
    "    scale=ToggleButtons(\n",
    "        options=[\"linear\", \"log\"], value=\"linear\"\n",
    "    ),\n",
    "    \n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "287c48771e214fbd9260cf1a7dd8639f",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "interactive(children=(IntSlider(value=15, continuous_update=False, description='itime', max=50, min=15), Outpu…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "<function ipywidgets.widgets.interaction._InteractFactory.__call__.<locals>.<lambda>(*args, **kwargs)>"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "interact(\n",
    "    tdem.plot_electric_currents, \n",
    "    itime=IntSlider(min=15, max=50, step=1, value=15, continuous_update=False)\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "215f8efbb7c54d0db76355ddd11f9eb4",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "interactive(children=(IntSlider(value=15, continuous_update=False, description='itime', max=50, min=15), Outpu…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "<function ipywidgets.widgets.interaction._InteractFactory.__call__.<locals>.<lambda>(*args, **kwargs)>"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "interact(\n",
    "    tdem.plot_magnetic_flux, \n",
    "    itime=IntSlider(min=15, max=50, step=1, value=15, continuous_update=False)\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
